package jsi.search;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import jsi.spider.WpArt;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;

import com.thoughtworks.xstream.XStream;

public class Indexer {
	
	XStream xstream = new XStream();
	List<Article> batchList = null;
	List<Article> incrementalList = null;
	List<WpArt> processed = null;
	Log log = LogFactory.getLog(this.getClass());
	
	public Indexer() {
		try {
			processed = (List<WpArt>) xstream.fromXML(new FileInputStream("etc/wp_articles/processed.xml"));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	
	public void init() throws FileNotFoundException{
		batchList = (List<Article>) xstream.fromXML(new FileInputStream("etc/batch.xml"));
		incrementalList = (List<Article>) xstream.fromXML(new FileInputStream("etc/incremental.xml"));
	
	}
	
	public void indexBatch() throws CorruptIndexException, LockObtainFailedException, IOException{
		Directory index = FSDirectory.getDirectory(new File("etc/lucene_index"));
		IndexWriter w = new IndexWriter(index, new PolishAnalyzer(), true);
		// new StandardAnalyzer()
		for (Article a:batchList){
			addArticle(w, a);
		}
		for (WpArt a:processed){
			addWpArt(w, a);
		}
		w.close();
	}

	private void addArticle(IndexWriter w, Article a) throws CorruptIndexException, IOException {
		Document doc = new Document();
		doc.add(new Field("author", a.getAuthor(), Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("title", a.getTitle(), Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("text", a.getText(), Store.NO, Field.Index.TOKENIZED));
		w.addDocument(doc);
	}
	
	private void addWpArt(IndexWriter w, WpArt a) throws CorruptIndexException, IOException {
		log.debug("wpArt.url: " + a.getUrl());
		a.denull();
		Document doc = new Document();
		doc.add(new Field("url", a.getUrl().toString(), Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("title", a.getTitle().toString(), Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("text", a.getText().toString(), Store.NO, Field.Index.TOKENIZED));
		doc.add(new Field("strong", a.getStrong().toString(), Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("b", a.getB().toString(), Store.YES, Field.Index.TOKENIZED));
		w.addDocument(doc);
	}

	public void indexIncremental() throws IOException {
		Directory index = FSDirectory.getDirectory(new File("etc/lucene_index"));
		IndexWriter w = new IndexWriter(index, new PolishAnalyzer(), false);
		for (Article a:incrementalList){
			addArticle(w, a);
		}
		w.close();
	}
	
	public void search(String querystr, String field) throws ParseException, IOException{
		Directory index = FSDirectory.getDirectory(new File("etc/lucene_index"));
		Query q = new QueryParser(field, new PolishAnalyzer()).parse(querystr);
		
		IndexSearcher s = new IndexSearcher(index);
		Hits hits = s.search(q);
		System.out.println("--------search: " + querystr + " " + hits.length());
		for (int i = 0; i < hits.length(); i++){
			Document doc = hits.doc(i);
			List<Field> fields = doc.getFields();
			System.out.println("**");
			for (Field f:fields){
				System.out.println("field: " + f.name() + " " + f.stringValue());
			}
		}
	}

//	public void createDocuments() throws FileNotFoundException {
//		List<Article> l = new LinkedList<Article>();
//		l.add(new Article(1,"Jakub", "Lucene tutorial", "To jest króciutki tutorial z wyszukiwania za pomocą narzędzia Lucene. Przetestuję polskie literki, polski słownik z kurnika, odmianę gramatyczną i pomyłki w wyrazach. Ciekawe tylko jak.", "tutorial howto lucene programowanie java", 1, 1, 1));
//		xstream.toXML(l, new FileOutputStream("etc/batch.xml"));
//		xstream.toXML(l, new FileOutputStream("etc/incremental.xml"));
//	}
	
}
